load motor.mat;
XtrainNorm = motorNormalize(Xtrain);
XtrainNorm = XtrainNorm(1:7, :);
Ytrain = Ytrain(1:7, :);

X = linspace(-1, 1);
X = X'
YMeanPrediction = ones(size(X), 6);
Phi = zeros(size(X), 20);
for i = 1:20
   Phi(:, i) = X;
end

for j = 0:19
    Phi(:, j+1) = Phi(:, j+1).^j; 
end

W = zeros(20, 20);

numDataPoints = size(XtrainNorm,1);
for Mindex=1:20
    powerIndex = Mindex;
    designMatrix = ones(numDataPoints,powerIndex);
    for rowDesign=1:numDataPoints
        for colDesign=1:powerIndex
            designMatrix(rowDesign,colDesign)=XtrainNorm(rowDesign,1).^(colDesign-1);
        end
    end
    w = (designMatrix' * designMatrix)\designMatrix' * Ytrain;
    w(20,1) = 0;
    W(:, Mindex) = w;
    
    if Mindex-1 == 0
        YMeanPrediction(:, 1) = Phi*w;
    end
    
    if Mindex-1 == 1
        YMeanPrediction(:, 2) = Phi*w;
    end

    if Mindex-1 == 3
        YMeanPrediction(:, 3) = Phi*w;
    end
    
    if Mindex-1 == 5
        YMeanPrediction(:, 4) = Phi*w;
    end
    
    if Mindex-1 == 19
        YMeanPrediction(:, 5) = Phi*w;
    end
    
    if Mindex-1 == 12
        YMeanPrediction(:, 6) = Phi*w;        
    end
end

figure(22)
subplot(2,3,1)
plot(X, YMeanPrediction(:, 1))
subplot(2,3,2)
plot(X, YMeanPrediction(:, 2))
subplot(2,3,3)
plot(X, YMeanPrediction(:, 3))
subplot(2,3,4)
plot(X, YMeanPrediction(:, 4))
subplot(2,3,5)
plot(X, YMeanPrediction(:, 5))

figure(232)
subplot(1,2,1)
plot(X, YMeanPrediction(:, 5))
subplot(1,2,2)
plot(X, YMeanPrediction(:, 6))
